﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadProps
{
    static class Helper
    {
        public static T ThrowIfNull<T>(this T argument, string name)
        {
            if (argument == null)
            {
                throw new ArgumentNullException(name);
            }
            return argument;
        }

        public static IEnumerable<ClrMember> GetFieldsAndProperties(this Type type, System.Reflection.BindingFlags bindingFlags)
        {
            return type.GetFields(bindingFlags).Select(f => new ClrMember(f))
                .Union(type.GetProperties(bindingFlags).Select(p => new ClrMember(p)));
        }

        public static void Raise<TArgs>(this EventHandler<TArgs> @event, object sender, TArgs args) where TArgs : EventArgs
        {
            if (@event != null)
            {
                @event(sender, args);
            }
        }

        public static void Raise(this System.ComponentModel.PropertyChangedEventHandler @event,
    object sender, string propName)
        {
            if (@event != null)
            {
                @event(sender, new System.ComponentModel.PropertyChangedEventArgs(propName));
            }
        }

        public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> items)
        {
            if (items == null)
            {
                return Enumerable.Empty<T>();
            }
            return items;
        }

        public static System.Collections.IEnumerable EmptyIfNull(this System.Collections.IEnumerable items)
        {
            if (items == null)
            {
                return Enumerable.Empty<object>();
            }
            return items;
        }
    }
}
